package me.hl.mybatis.respository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import me.hl.mybatis.domain.Employees;
import me.hl.mybatis.mapper.EmployeesMapper;
import me.hl.mybatis.respository.IEmployeeRepository;
import me.hl.mybatis.respository.base.BaseRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Planck on 2016/1/21.
 */
@Repository
public class EmployeeRepository extends BaseRepository<Employees> implements IEmployeeRepository {

    private static Logger logger = LoggerFactory.getLogger(EmployeeRepository.class);

    @Resource
    EmployeesMapper employeesMapper;

    @Override
    public Employees get(long uid) {
        Example example = new Example(Employees.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", uid);

        List<Employees> emps = getMapper().selectByExample(example);
        if (!emps.isEmpty()) {
            return emps.get(0);
        } else {
            return null;
        }
    }

    @Override
    public List<Employees> departmentEmployees(long departmentid) {
        Example example = new Example(Employees.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("departmentid", departmentid);
        //example.setOrderByClause("id DESC");
        PageHelper.startPage(1, 5);
        List<Employees> employees = getMapper().selectByExample(example);
        PageInfo page = new PageInfo(employees);
        logger.info("总条数=[{}],总页数=[{}]", page.getTotal(), page.getPages());
        return employees;

    }

    @Override
    public Employees getFirst() {
        return employeesMapper.selectFirst();
    }

    @Override
    public Employees selectOne(long id) {
        return employeesMapper.myselectOne(id);
    }


}
